<?php

namespace app\api\controller;

use app\http\TokenTrait;
use think\Controller;
use think\Db;
use think\Request;

class Login extends Controller
{
    use TokenTrait;

    /**
     * 用户登录
     * @return json 返回类型
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function login(Request $request)
    {
        $username = $request->param('username');
        $password = $request->param('password');
        $username = isset($username) ? $username : '';
        $password = isset($password) ? $password : '';
        if (empty($username) || empty($password)) {
            return json(['code' => 404, 'msg' => '账号或密码不能为空']);
        }
        $user = Db::name('admin')->where(array('username' => $username, 'password' => MD5($password)))->find();
        if (empty($user)) {
            return json(['code' => 404, 'msg' => '账号或密码输入错误']);
        }
        if ($user['status'] ==0) {
            $token = Db::name('token')->where('uid', $user['id'])->find();
            if (empty($token)) {
                $data = [
                    'uid' => $user['id'],
                    'token' => $this->setToken(),
                    'out_time' => time() + 7 * 24 * 3600//token有效期 7天
                ];
                Db::name('token')->insert($data);
            } else {
                $data = [
                    'uid' => $user['id'],
                    'token' => $this->setToken(),
                    'out_time' => time() + 7 * 24 * 3600//token有效期 7天
                ];
                Db::name('token')->where('uid', $user['id'])->update($data);
            }
            $arr =['end_time'=>time(),'ip'=>$request->ip()];
            Db::name('admin')->where('id',$user['id'])->setField($arr);
            return json(['code' => 200, 'msg' => '登录成功', 'data' => $data]);
        } else {
            return json(['code' => 404, 'msg' => '账号已被禁用,请联系管理员']);
        }
    }

    /**
     * 用户注册
     * @return json 返回类型
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function register()
    {
        $a = Request()->param('username');
        $b = Request()->param('password');
        $username = isset($a) ? $a : '';
        $password = isset($b) ? $b : '';
        if (empty($username) || empty($password)) {
            return json(['code' => 404, 'msg' => '用户名或密码不能为空']);
        }
        $current = Db::name('user')->where('username', $username)->find();
        if (!empty($current)) {
            return json(['code' => 404, 'msg' => '用户名已存在']);
        }
        $data = [
            'username' => $username,
            'password' => MD5($password),
            'create_time' => time()
        ];
        $result = Db::name('user')->insertGetId($data);
        if (!empty($result)) {
            return json(['code' => 200, 'msg' => '注册成功']);
        } else {
            return json(['code' => 404, 'msg' => '注册失败']);
        }
    }

    /**
     * 退出登录
     * @return json 返回类型
     */
    public function loginOut()
    {
        $id = Request()->param('id');
        $id = isset($id) ? $id : '';
        $out = Db::name('token')->where('uid', $id)->setField('token', '');
        if ($out) {
            return json(['code' => 200, 'msg' => '退出登录']);
        } else {
            return json(['code' => 404, 'msg' => '退出失败']);
        }
    }
}
